//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.tool.utils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBUtil {
    private static final String DATABASE_NAME = "DB.db";
    private static final String TABLE_QC = "QueryConfig";
    private static final int DATABASE_VERSION = 1;
    public static final String QC_KEY = "key";
    public static final String QC_VALUE = "value";
    public static final String QC_ROWID = "_id";
    public static final String QC_EXPAND = "expand";
    public static final String First = "first";
    private DBUtil.DatabaseHelper mDbHelper;
    public SQLiteDatabase mDb;
    private static final String QC_CREATE = "create table QueryConfig (_id integer primary key autoincrement, key text not null, value text , expand text );";
    private final Context mCtx;

    public DBUtil(Context ctx) {
        this.mCtx = ctx.getApplicationContext();
        DataUtil.dex();
    }

    private DBUtil open() throws SQLException {
        if(mDbHelper == null){
            this.mDbHelper = new DBUtil.DatabaseHelper(this.mCtx);
        }
        return this;
    }

    private void closeDB() {
        if(mDb!=null){
            this.mDb.close();
            this.mDb = null;
        }
    }

    public synchronized boolean deleteRecord(String key) {
        open();
        this.mDb = this.mDbHelper.getWritableDatabase();
        key = this.Encrypt(key);
        boolean result = this.mDb.delete("QueryConfig", "key= ?", new String[]{key}) > 0;
        closeDB();
        return result;
    }

    public synchronized boolean deleteRecordById(String id) {
        open();
        this.mDb = this.mDbHelper.getWritableDatabase();
        id = this.Encrypt(id);
        boolean result = this.mDb.delete("QueryConfig", "_id= ?", new String[]{id}) > 0;
        closeDB();
        return result;
    }

    public synchronized String getRecord(String key) throws SQLException {
        open();
        this.mDb = this.mDbHelper.getReadableDatabase();
        key = this.Encrypt(key);
        String value = "";
        Cursor mCursor = this.mDb.query(true, "QueryConfig", new String[]{"_id", "key", "value", "expand"}, "key= ?", new String[]{key}, (String)null, (String)null, (String)null, (String)null);
        if(mCursor!=null){
            mCursor.moveToFirst();
            if(mCursor.getCount() > 0) {
                value = mCursor.getString(mCursor.getColumnIndexOrThrow("value"));
                value = this.Decrypt(value);
            }
            mCursor.close();
        }
        closeDB();
        return value;
    }

    public synchronized boolean isRecordExist(String key) throws SQLException {
        open();
        this.mDb = this.mDbHelper.getReadableDatabase();
        boolean flag = false;
        Cursor mCursor = this.mDb.query(true, "QueryConfig", new String[]{"_id", "key", "value", "expand"}, "key= ?", new String[]{key}, null, null, null, null);
        if(mCursor!=null){
            mCursor.moveToFirst();
            if(mCursor.getCount() > 0) {
                flag = true;
            }

            mCursor.close();
        }
        closeDB();
        return flag;
    }

    public synchronized String getRecord(String key, String expand) throws SQLException {
        open();
        this.mDb = this.mDbHelper.getReadableDatabase();
        key = this.Encrypt(key);
        expand = this.Encrypt(expand);
        String value = "";
        Cursor mCursor = this.mDb.query(true, "QueryConfig", new String[]{"_id", "key", "value", "expand"}, "key= ? and expand= ?", new String[]{key, expand}, null, null, null, null);
        if(mCursor!=null){
            mCursor.moveToFirst();
            if(mCursor.getCount() > 0) {
                value = mCursor.getString(mCursor.getColumnIndexOrThrow("value"));
                value = this.Decrypt(value);
            }

            mCursor.close();
        }
        closeDB();
        return value;
    }

    public  void updateOrAddRecord(String key, String value, String expand) {
        if(this.isRecordExist(key)) {
            this.updateRecord(key, value, expand);
        } else {
            this.addRecord(key, value, expand);
        }
    }

    public void updateOrAddRecord(String key, String value) {
        if(this.isRecordExist(key)) {
            this.updateRecord(key, value);
        } else {
            this.addRecord(key, value);
        }
    }

    public synchronized long addRecord(String key, String value) {
        open();
        this.mDb = this.mDbHelper.getWritableDatabase();
        key = this.Encrypt(key);
        value = this.Encrypt(value);
        ContentValues initialValues = new ContentValues();
        initialValues.put("key", key);
        initialValues.put("value", value);
        long result = this.mDb.insert("QueryConfig", null, initialValues);
        closeDB();
        return result;
    }

    public synchronized long addRecord(String key, String value, String expand) {
        open();
        this.mDb = this.mDbHelper.getWritableDatabase();
        key = this.Encrypt(key);
        value = this.Encrypt(value);
        expand = this.Encrypt(expand);
        ContentValues initialValues = new ContentValues();
        initialValues.put("key", key);
        initialValues.put("value", value);
        initialValues.put("expand", expand);
        long result = this.mDb.insert("QueryConfig", null, initialValues);
        closeDB();
        return result;
    }

    public synchronized boolean updateRecord(String key, String value, String expand) {
        open();
        this.mDb = this.mDbHelper.getWritableDatabase();
        key = this.Encrypt(key);
        expand = this.Encrypt(expand);
        value = this.Encrypt(value);
        ContentValues args = new ContentValues();
        args.put("key", key);
        args.put("value", value);
        boolean result = this.mDb.update("QueryConfig", args, "key= ? and expand=?", new String[]{key, expand}) > 0;
        closeDB();
        return result;
    }

    public synchronized boolean updateRecord(String key, String value) {
        open();
        this.mDb = this.mDbHelper.getWritableDatabase();
        key = this.Encrypt(key);
        value = this.Encrypt(value);
        ContentValues args = new ContentValues();
        args.put("key", key);
        args.put("value", value);
        boolean result = this.mDb.update("QueryConfig", args, "key= ?", new String[]{key}) > 0;
        closeDB();
        return result;
    }

    private String Encrypt(String input) {
        return input;
    }

    private String Decrypt(String input) {
        return input;
    }

    public static class DatabaseHelper extends SQLiteOpenHelper {
        public DatabaseHelper(Context context) {
            super(context, "DB.db", null, 1);
        }

        public void onCreate(SQLiteDatabase db) {
            db.execSQL("create table QueryConfig (_id integer primary key autoincrement, key text not null, value text , expand text );");
        }

        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS QueryConfig");
            this.onCreate(db);
        }
    }
}
